<?php
class Manager_News
{
	// FIXME: change name to setTags
	public static function addTags(News $news, array $tags = array())
	{
		try {
			foreach ($tags as $tag) {
				if ($tag instanceof Tags) {
					$newsTags = new Newstags();
					$newsTags->setNews($news)
						->setTags($tag)
						->save();
				}
			}
		} catch (Exception $e) {
			throw new Exception('Could not add tags into news. ' . $e->getMessage());
		}
	}
	
	public static function onClick(News $news)
	{
		if (isset($_SESSION['clicks'][$news->getIdNews()])) {
			// don't count
			return;
		}
		
		if (!isset($_SESSION['clicks'])) {
			$_SESSION['clicks'] = array();
		}
		
		$vortal = $news->getVortals();
		$category = $news->getCategories();
		$connection = Propel::getConnection(); 
		$connection->beginTransaction();
		
		try {
			$counter =	$news->getClickcounter() + 1;
			$news->setClickcounter($counter);
			$news->save();
			
			$counter = $vortal->getClickcounter() + 1;
			$vortal->setClickcounter($counter);
			$vortal->save();
			
			$counter = $category->getClickcounter() + 1;
			$category->setClickcounter($counter);
			$category->save();
			
			if (Manager_User::isLogged()) {
				Manager_User::onClick($category);
			}
			
			$connection->commit();
			$_SESSION['clicks'][$news->getIdNews()] = true;
		} catch (Exception $e) {
			$connection->rollBack();
			
			throw new Exception('Could not update click counters. ' . $e->getMessage());
		}
	}
	
	public static function onRead(News $news)
	{
		self::onClick($news);
		return header('Location: ' . $news->getUrlOriginal());
	}
	
	public static function getBestCategory(News $news)
	{
		// FIXME: use propel methods
		$connection = Propel::getConnection();
		
		$sql = 'SELECT `id_category`, COUNT(*) 
			FROM `CategoriesTags` INNER JOIN `NewsTags` ON `CategoriesTags`.`id_tag` 
			= `NewsTags`.`id_tag` WHERE `id_news` = ' .
			$news->getIdNews()
			. ' GROUP BY `id_category` LIMIT 1';
			
		$stmt = $connection->prepare($sql);
		$stmt->execute();
		$result = $stmt->fetchAll();
		//var_dump($result);
		if (isset($result[0]['id_category'])) {
			$category = CategoriesPeer::retrieveByPK($result[0]['id_category']);
			if (!empty($category)) {
				return $category;
			}
		}

		// return default
		return Manager_Category::getDefault(); 
	}
	
	public static function rate(News $news, Users $user, $rate)
	{
		// FIXME: check if user rated earlier
		//$newsRating = NewsratingstatisticsPeer::retrieveByPK($user->getIdUser(), $news->getIdNews());
		$criteria = new Criteria();
		$criteria->add(NewsratingstatisticsPeer::ID_NEWS, $news->getIdNews(), Criteria::EQUAL)
			->add(NewsratingstatisticsPeer::ID_USER, $user->getIdUser(), Criteria::EQUAL);
		
		$newsRating = NewsratingstatisticsPeer::doSelectOne($criteria);
		
		if (!empty($newsRating)) {
			// user juz zaglosowal na ten news
			return;
		}
		
		$newsRating = new Newsratingstatistics();
		$newsRating->setUsers($user)
			->setNews($news)
			->setIsPositive($rate)
			->setDate(time())
			->save();
		
		$counter = $user->getRatingcounter();
		$user->setRatingcounter($counter + 1)
			->save();
	}
	
	public static function getRating(News $news)
	{
		return new NewsRating($news);
	}
}